// ActionScript file
package com.wdxc {
    import com.wdxc.util.JPGEncoder;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest; 
    import flash.net.URLRequestMethod ;
    import flash.net.URLVariables;
      
    /**
    * 与服务端通信
    * @author luqinglong
    */
    public class ImageService {   
                 
        private var url:String = "/photo/PhotoEditServlet" ;    
        //private var url:String = "http://localhost:8080//photo/PhotoEditServlet" ;
            
         
        private var _photoid:String ;
        //?photoid=267005
         
        /**
         * 照片id
         */
        public function set photoid(id:String):void {
            this._photoid = id ;
        }
        public function get photoid():String {
            return this._photoid ;
        }
        public function ImageService() {
            
        }
          
        /**
         * 
         * @param   type 判断是否裁剪或者旋转
         * @param   photoid 照片id
         * @param   x  
         * @param   y
         * @param   width
         * @param   height
         */   
        public function submitCut(x:Number,y:Number,width:Number,height:Number,scale:String):void {
            //url = url+"?"       
            //url = url+"?radom="+Math.floor(Math.random()*1000)
            var request:URLRequest = new URLRequest(url);  
            var param:URLVariables = new URLVariables();      
            param.random = Math.floor(Math.random() * 100);
            param.photoid = _photoid ; 
            param.type = Constant.OPERATE_CUT ;      
            param.photoid = photoid ;     
            param.scaleInfo = scale ;     
            param.x = x ;
            param.y = y ;
            param.width = width ;
            param.height = height ;
            
            request.data = param ;
            
                        
            var loader:URLLoader = new URLLoader();    
            //loader.
                   
            loader.load(request) ;  
            loader.addEventListener(Event.COMPLETE, completeHandler) ;  
            //loader.addEventListener(Event.)
        }
          
        /**
         * 保存旋转
         * @param   ag
         */
        public function submitRotation(ag:Number):void {
            var request:URLRequest = new URLRequest(url);  
            var param:URLVariables = new URLVariables();          
            param.random = Math.floor(Math.random() * 1000);
            param.photoid = _photoid  ; 
            param.type = Constant.OPERAT_ROTATION ;      
            param.photoid = photoid ;
            param.angle = ag ; 
            request.data = param ;
            
                        
            var loader:URLLoader = new URLLoader();    
            //loader.
              
            loader.load(request) ;  
            loader.addEventListener(Event.COMPLETE, completeHandler) ;  
        
        }
              
        /**  
         * 对图片编码并提交给服务端保存。
         * @param bitmapdata  
         * @param url       
         */          
        /*
        public function submit(bitmapdata:BitmapData, url:String):void {
            
            
            var request:URLRequest = new URLRequest(url);  
              
            

            var loader:URLLoader = new URLLoader();                  
            loader.load(request) ;            
                  
            loader.addEventListener(Event.COMPLETE, completeHandler) ;  
        }*/       
                                        
        private function completeHandler(e:Event):void {
               
            Main.SCENE.removeBitmap();    
                 
            Main.SCENE.startLoadPic();           
            
            e.currentTarget.removeEventListener(Event.COMPLETE, completeHandler);   
        }  
    }
    
}